//
//  MQOrderDetailViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/24.
//  Copyright © 2017年 mq. All rights reserved.
//

//订单详情
#import "MQOrderDetailViewController.h"
#import "MQOrderProductCell.h"
#import "MQOrderDetailadderssTableViewCell.h"
#import "MQOrderBoxCell.h"
#import "MQBaseUserInfoModel.h"
#import "MQSubOrders.h"
#import "MQOrderListmodel.h"
@interface MQOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *orderdetailtableview;

@property(nonatomic,strong)MQOrderListmodel *modelss;

@end

@implementation MQOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"订单详情";
    
    _modelss = [MQOrderListmodel new];
//    MQOrderListmode
    [MQOrderListmodel mj_setupObjectClassInArray:^NSDictionary *{
        return @{
                 @"orders" : @"MQSubOrders"
                 };
    }];
    [self setUpMainInterface];
    
    [self getdetailinfo];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpMainInterface{
    
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    
    [self.view addSubview:self.orderdetailtableview];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section ==0) {
        MQOrderProductCell *cell = [MQOrderProductCell cellWithTableview:tableView];
        if (_modelss) {
            [cell.productimageview sd_setImageWithURL:[NSURL URLWithString:_modelss.productPic]];
            cell.productnamelabel.text = _modelss.productName;
            cell.youfeiLabel.text = [NSString stringWithFormat:@"邮费 ¥%@",_modelss.deliveryCost];
            cell.buyerLabel.text = [NSString stringWithFormat:@"购买人 %@",_modelss.buyer];
            cell.uintpricelabel.text = [NSString stringWithFormat:@"%@",_modelss.productPrice];
            cell.countLabel.text = [NSString stringWithFormat:@"x%@",_modelss.productCount];
            cell.totalpriceLabel.text = [NSString stringWithFormat:@"¥%@",_modelss.payTotal];
            cell.ordercodelabel.text = [NSString stringWithFormat:@"订单编号 %@",_modelss.ID];
            cell.ordertimelabel.text = [NSString stringWithFormat:@"下单时间: %@",_modelss.createTime];
            
            if ([_modelss.payType isEqualToString:@"alipay"]) {
                cell.paytypeLabel.text = [NSString stringWithFormat:@"支付方式: 支付宝支付"];
                cell.paytimeLabel.text = [NSString stringWithFormat:@"支付时间: %@",_modelss.payTime];
            }else{
                cell.paytypeLabel.text = [NSString stringWithFormat:@"支付方式: 未支付"];
//                cell.paytimeLabel.text = [NSString stringWithFormat:@"支付时间: %@",_modelss.payTime];
                cell.paytimeLabel.hidden = YES;
            }
            
        }
        
        
        return cell;
    }else{
        
        if (indexPath.row == 0) {
            MQOrderDetailadderssTableViewCell *cell = [MQOrderDetailadderssTableViewCell cellWithTableview:tableView];
            
            cell.souhuorenLabel.text = [NSString stringWithFormat:@"收货人 %@",_modelss.buyer];
            
           NSString *string = [NSString stringWithFormat:@"%@%@%@",_modelss.province,_modelss.city,_modelss.area];
            if (![_modelss.addr isKindOfClass:[NSNull class]] && _modelss.addr) {
                string = [string stringByAppendingString:_modelss.addr];
            }
            cell.addersslabel.text = string;
            cell.phoneNumberLabel.text = _modelss.telephone;
            return cell;
        }else{
            MQOrderBoxCell *cell = [MQOrderBoxCell cellwithtableview:tableView];
            
            MQSubOrders *model = _modelss.orders[indexPath.row - 1];
            
            
            cell.ordercodeslabel.text = model.subNum;
            cell.priceLabels.text = [NSString stringWithFormat:@"产品金额: ¥%@",model.productCost];
            cell.canpingcountlabel.text = [NSString stringWithFormat:@"产品数量: %@",model.productCount];
            cell.youfeiLabels.text = [NSString stringWithFormat:@"邮费: ¥%@",model.deliveryCost];
            
            if (![model.deliveryTime isKindOfClass:[NSNull class]] && model.deliveryTime) {
                cell.fahuotimeLabel.text = [NSString stringWithFormat:@"发货时间 %@",model.deliveryTime];
                cell.codenumberLabel.text = [NSString stringWithFormat:@"%@ %@",model.deliveryCom,model.deliveryNum];
                cell.codenumberLabel.hidden = NO;
                
            }else{
                cell.fahuotimeLabel.text = @"暂未发货";
//                cell.codenumberLabel.text = [NSString stringWithFormat:@"%@ %@",model.deliveryCom,model.deliveryNum];
                cell.codenumberLabel.hidden = YES;
            }
            
             return cell;
        }
       
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }else{
        return _modelss.orders.count + 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 7.f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerview = [UIView new];
    headerview.backgroundColor = BSTColorFromRGB(0xefefef);
    return headerview;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        return 195.f;
    }else{
        if (indexPath.row == 0) {
           return 95.f;
        }else{
            return 116.f;
        }
    }
}

-(void)getdetailinfo{
    
    MQBaseUserInfoModel *userinfomodel = [MQBaseUserInfoModel shareInstant];
    [userinfomodel loadUserinfoFromSadeBox];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:_ordermodel.ID forKey:@"orderId"];
    [param setValue:userinfomodel.userId forKey:@"userId"];
    
    [MQRequestTools GETWithMethod:orderInfo_info params:param success:^(id json) {
        NSLog(@"%@",json);
        NSDictionary *dic = [json valueForKey:@"data"];
        _modelss = [MQOrderListmodel mj_objectWithKeyValues:dic];
        [_orderdetailtableview reloadData];
        _orderdetailtableview.hidden = NO;
    } failure:^(NSError *error) {
        
    }];
    
    
    
}

-(UITableView *)orderdetailtableview{
    if (!_orderdetailtableview) {
        
        
        _orderdetailtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BSTScreenW, BSTScreenH) style:UITableViewStylePlain];
        _orderdetailtableview.delegate = self;
        _orderdetailtableview.dataSource = self;
        _orderdetailtableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _orderdetailtableview.showsVerticalScrollIndicator = NO;
        UIView *footerview = [UIView new];
        _orderdetailtableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        footerview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _orderdetailtableview.tableFooterView = footerview;
        _orderdetailtableview.hidden = YES;
    }
    return _orderdetailtableview;
}
@end
